package com.linkhub.oms.admin.modules.logistic.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.linkhub.oms.admin.modules.logistic.domain.dto.ShippingChannelDTO;
import com.linkhub.oms.admin.modules.logistic.service.LogisticChannelBizService;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.dao.mpservice.ILogisticService;
import com.linkhub.oms.dao.mpservice.IShippingLogisticsMappingService;
import com.linkhub.oms.entity.LogisticEntity;
import com.linkhub.oms.entity.ShippingLogisticsMappingEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: LogisticChannelBizService
 * @Author: echo
 * @Date: 2024/5/25 11:32
 * @Version: 1.0
 */
@Slf4j
@Service
public class LogisticChannelBizServiceImpl implements LogisticChannelBizService {

    @Resource
    private IShippingLogisticsMappingService shippingLogisticsMappingService;

    @Resource
    private ILogisticService logisticService;

    @Override
    public List<ShippingChannelDTO> listShopShippingChannels(String sysShopId, String shippingMethod) {
        List<ShippingLogisticsMappingEntity> logisticsMappingList = shippingLogisticsMappingService.listShippingChannels(sysShopId, shippingMethod);
        if (CollUtil.isNotEmpty(logisticsMappingList)) {
            List<String> channelCodes = QtStreamUtil.map(logisticsMappingList,
                    ShippingLogisticsMappingEntity::getChannelCode);

            List<LogisticEntity> logistics = logisticService.listByChannelCodes(channelCodes);
            return ShippingChannelDTO.map(logistics);
        }

        List<LogisticEntity> channels = logisticService.listEnableChannels();
        return ShippingChannelDTO.map(channels);
    }
}
